/**
 * 忘记密码
 * @author  Jello
 * @create  2018/10/16 10:45
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var spinner = new Spinner(opts);
var token = ""; //获取验证码成功返回的token

$(function() {

	//获取验证码
	$("#code_btn").click(function() {
		var phoneNum = $("#phoneNum").val(); //手机号
		var pwd = $("#pwd_input").val(); //密码
		var comfirmPwd = $("#pwd_confirm").val(); //确认密码

		var checkValue = (checkPhone(phoneNum) && checkPwd(pwd, 2) && checkConfirmPwd(pwd, comfirmPwd));
		if(checkValue) {
			var param = {};
			param.phone = phoneNum;
			param.type = 1;
			//获取验证码接口上送
			gainCode(param, function(data) {
				token = data;
				//开启验证码定时器
				for(i = 1; i <= 60; i++) {
					if(i == 1) {
						window.setTimeout("countDown(" + i + ")");
					} else {
						window.setTimeout("countDown(" + i + ")", (i - 1) * 1000);
					}
				}
			});
		}
	})

	//确认
	$("#forget_btn").click(function() {
		var phoneNum = $("#phoneNum").val(); //手机号
		var pwd = $("#pwd_input").val(); //密码
		var comfirmPwd = $("#pwd_confirm").val(); //确认密码
		var code = $("#code").val(); //验证码

		var checkValue = checkPhone(phoneNum) && checkPwd(pwd, 2) && checkConfirmPwd(pwd, comfirmPwd) && checkCode(code);
		if(checkValue) {
			var param = {};
			param.phone = phoneNum;
			param.type = 1;
			param.code = code;
			param.token = token;
			//校验验证码接口上送
			mateCode(param, function() {
				//忘记密码
				$.ajax({
					type: "post",
					url: serverUrl + "/login/set_psw",
					data: "{\"phone\":\"" + phoneNum + "\",\"password\":\"" + hex_md5(hex_md5(pwd) + salt) + "\"}",
					async: false,
					cache: false,
					contentType: "application/json; charset=utf-8",
					dataType: "json",
					timeout: 15000,
					crossDomain: true,
					success: function(data) {
//						console.log("忘记密码接口：" + JSON.stringify(data));
						spinner.spin();
						var forgetObj = eval(data);
						if(forgetObj.code == 0 && forgetObj.data == 1) {
							mui.toast("密码修改成功");
							setTimeout(function() {
								window.location.href = "login.html";
							}, 1000);
						} else {
							mui.toast(forgetObj.msg);
							return;
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						spinner.spin();
						if(status == 'timeout') {
							mui.toast("请求超时");
						} else {
							mui.toast("请求网络服务数据失败");
						}
						return;
					}
				});
			})
		}
	})
})

/**
 * 校验确认密码
 * @param {Object} pwd 密码
 * @param {Object} comfirmPwd 确认密码
 */
function checkConfirmPwd(pwd, comfirmPwd) {
	if(comfirmPwd == "") {
		mui.toast('确认密码不能为空');
		return;
	} else if(comfirmPwd.indexOf(" ") >= 0) {
		mui.toast('确认密码中不能有空格字符');
		return;
	} else if((/[\u4e00-\u9fa5]+/).test(comfirmPwd)) {
		mui.toast('确认密码不能包含中文');
		return;
	} else if(pwd != comfirmPwd) {
		mui.toast('密码不一致');
		return;
	} else if(comfirmPwd.length < 6 || comfirmPwd.length > 16) {
		mui.toast("密码长度为6-16位");
		return;
	}
	return true;
}

/**
 * 输入密码切换显示 
 */
document.getElementById("pwd_input_show").addEventListener("tap", function() {
	var type = $("#pwd_input").prop("type")
	if("password" == type) {
		$("#pwd_input").prop("type", "text");
		$("#pwd_input_show").attr("src", "img/login/regist_03.jpg");
	} else {
		$("#pwd_input").prop("type", "password");
		$("#pwd_input_show").attr("src", "img/login/login_22.jpg");
	}
}, false)

/**
 * 确认密码切换显示 
 */
document.getElementById("pwd_confirm_show").addEventListener("tap", function() {
	var type = $("#pwd_confirm").prop("type")
	if("password" == type) {
		$("#pwd_confirm").prop("type", "text");
		$("#pwd_confirm_show").attr("src", "img/login/regist_03.jpg");
	} else {
		$("#pwd_confirm").prop("type", "password");
		$("#pwd_confirm_show").attr("src", "img/login/login_22.jpg");
	}
}, false)